package com.ysx.server.controller.admin;

import com.github.pagehelper.Page;
import com.ysx.server.pojo.dto.CategoryPageQueryDTO;
import com.ysx.server.pojo.entity.Category;
import com.ysx.server.pojo.vo.PageQueryVO;
import com.ysx.server.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryManageController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/category")
    public PageQueryVO<Category> categoryPageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("category分页查询：{}",categoryPageQueryDTO);
        if(categoryPageQueryDTO==null){
            return null;
        }
        Page<Category> page = categoryService.page(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> result = page.getResult();
        return new PageQueryVO<Category>(total,result);
    }

    /**
     * 保存分类信息
     * @param category
     * @return
     */
    @PostMapping("/save")
    public boolean save(@RequestBody Category category){
        if(category==null){
            return false;
        }
        return categoryService.save(category);
    }

    /**
     * 更新分类信息
     * @param category
     * @return
     */
    @PostMapping("/update")
    @CacheEvict(cacheNames = {"goodsCache_new","goodsCache_all","goodsCache"},allEntries=true)
    public boolean update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        if(category==null){
            return false;
        }
        return categoryService.updateCategory(category);
    }

    /**
     * 删除分类信息
     * @param id
     * @return
     */
    @GetMapping("/delCategory/{id}")
    public boolean delCategory(@PathVariable Long id){
        if(id==null||id<0){
            return false;
        }
        return categoryService.delCategory(id);
    }
}
